package com.agency8x.model;

import com.agency8x.model.base.BaseTransactionDetailModel;
import com.agency8x.util.DateTimeUtil;
import com.agency8x.util.RegexUtil;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;

public class TransactionDetailModel extends BaseTransactionDetailModel<TransactionDetailModel> {

	/**
	 * 资金交易明细
	 */
	private static final long serialVersionUID = 1L;
	public static final TransactionDetailModel me = new TransactionDetailModel().dao();

	public Page<TransactionDetailModel> paginate(int pageNumber, int pageSize, String searchword, String beginDate, String endDate) {
        String sql = "select d.*, u.nickname ";
        String sqlFrom = "from t_finance_transaction_details d left join t_user u on d.user_id=u.user_id";
        sqlFrom+=" where d.trade_type in (1,2,3) ";
        if(DateTimeUtil.validateDate(beginDate)){
            sqlFrom = sqlFrom + " and d.create_time > '" + beginDate + " 00:00:00'";
        }
        if (DateTimeUtil.validateDate(endDate)) {//起始
            sqlFrom = sqlFrom + " and d.create_time < '" + endDate + " 23:59:59'";
        }

        if (StrKit.notBlank(searchword)) {
            if (RegexUtil.isNumeric(searchword)) {
                sqlFrom = sqlFrom + " and (u.user_id like '%" + searchword+"%' or u.nickname like '%"+searchword+"%')";
            }else{
                sqlFrom = sqlFrom + " and u.nickname like '%" + searchword+"%'";
            }
        }
        sqlFrom = sqlFrom + " order by d.id desc";
        return paginate(pageNumber, pageSize, sql, sqlFrom);
    }

	public String getTradeTypeText() {
		Short trade_type = getTradeType();
		String stype = "";
		// 1:充值,2:返现;3:提现;4:赠送;5:领取;6:转换
		switch (trade_type) {
		case 1:
			stype = "充值";
			break;
		case 2:
			stype = "返现";
			break;
		case 3:
			stype = "提现";
			break;
		case 4:
			stype = "赠送";
			break;
		case 5:
			stype = "领取";
			break;
		case 6:
			stype = "转换";
			break;
		}
		return stype;
	}

	/**
	 * 获取顶级代理所有下线的账户明细
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @param top_agent_id
	 * @param word
	 * @param date1
	 * @param date2
	 * @return
	 * @since 1.0 2017-5-27
	 */
	public Page<TransactionDetailModel> getPaginateByTopAgentId(int pageNumber, int pageSize, Integer top_agent_id,
			String word, String date1, String date2) {
		return getPaginateByUser(pageNumber, pageSize, "top_agent_id=" + top_agent_id, word, date1, date2);
	}

	public Page<TransactionDetailModel> getPaginateByUserId(int pageNumber, int pageSize, Integer user_id, String word,
			String date1, String date2) {
		return getPaginateByUser(pageNumber, pageSize, "user_id=" + user_id, word, date1, date2);
	}

	public Page<TransactionDetailModel> getPaginateByParent1Uid(int pageNumber, int pageSize, Integer parent1_uid,
			String word, String date1, String date2) {
		return getPaginateByUser(pageNumber, pageSize, "parent1_uid=" + parent1_uid, word, date1, date2);
	}

	public Page<TransactionDetailModel> getPaginateByParent2Uid(int pageNumber, int pageSize, Integer parent2_uid,
			String word, String date1, String date2) {
		return getPaginateByUser(pageNumber, pageSize, "parent2_uid=" + parent2_uid, word, date1, date2);
	}

	private Page<TransactionDetailModel> getPaginateByUser(int pageNumber, int pageSize, String userCondition,
			String word, String date1, String date2) {
		String sql = "SELECT u.user_id, u.nickname, d.id, d.serial_no, d.out_trade_no, d.trade_type, d.amount, d.pay_channel, d.blance, d.create_time, d.remark, d.auditor_id";
		String sqlFrom = "FROM dbo.t_finance_transaction_details d LEFT JOIN t_user u ON d.user_id=u.user_id";
		String sqlWhere = " WHERE u." + userCondition + " and d.trade_type in (1,2,3)";
		String sqlOrder = " ORDER BY d.id desc";
		if (StrKit.notBlank(date1) && DateTimeUtil.validateDate(date1)) {
			sqlWhere += " and d.create_date >='" + date1 + "'";
		}
		if (StrKit.notBlank(date2) && DateTimeUtil.validateDate(date2)) {
			sqlWhere += " and d.create_date <='" + date2 + "'";
		}

		if (StrKit.notBlank(word)) {
			sqlWhere += " and (u.user_id like '%" + word + "%' or u.nickname like '%" + word + "%')";
		}
		sqlFrom += sqlWhere + sqlOrder;
		return paginate(pageNumber, pageSize, sql, sqlFrom);
	}

	public String getCreateTimeText() {
		return DateTimeUtil.formatSqlServerDateTime(getCreateTime());
	}

}
